﻿using System;
using System.Collections.Generic;

namespace Wolfpack.Contrib.Checks.Powershell.Scripting
{
    public class PSHealthCheckData
    {
        /// <summary>
        /// This is used by a HealthCheck to inform the Wolfpack infrastructure how
        /// to shape the stream of alerts emitted
        /// </summary>
        public string NotificationMode { get; set; }

        /// <summary>
        /// The result of the check if it has a true/false outcome
        /// </summary>
        public bool? Result { get; set; }

        /// <summary>
        /// The count result of the check
        /// </summary>
        public double? ResultCount { get; set; }

        /// <summary>
        /// This tells Wolfpack how to display your ResultCount property when visualised
        /// in the Notification Activity UI (if your check sets ResultCount otherwise leave blank).
        /// </summary>
        public string DisplayUnit { get; set; }

        /// <summary>
        /// Indicates a critical failure in the check. The details
        /// about the failure are stored in the <see cref="CriticalFailureDetails"/>
        /// property
        /// </summary>
        public bool CriticalFailure { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public PSCriticalFailureDetails CriticalFailureDetails { get; set; }

        /// <summary>
        /// Identifies the health check
        /// </summary>
        public PSPluginDescriptor Identity { get; set; }

        /// <summary>
        /// Indicates when the check took place
        /// </summary>
        public DateTime GeneratedOnUtc { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public PSGeoData Geo { get; set; }

        /// <summary>
        /// How long the check took to execute
        /// </summary>
        public TimeSpan Duration { get; set; }

        /// <summary>
        /// General text field to help describe the result
        /// </summary>
        public string Info { get; set; }

        /// <summary>
        /// General text field to help categorise the result
        /// </summary>
        public List<string> Tags { get; set; }

        /// <summary>
        /// Property bag to contain name/value pairs of result data
        /// </summary>
        public PSResultProperties Properties { get; set; }

        public PSHealthCheckData()
        {
            GeneratedOnUtc = DateTime.UtcNow;
            Properties = new PSResultProperties();
        }

        public void AddProperty(string name, string value)
        {
            Properties.Add(name, value);
        }

        public void SetGeoLocation(string latitude, string longitude)
        {
            Geo = new PSGeoData
                      {
                          Latitude = latitude,
                          Longitude = longitude
                      };
        }

        public void SetIdentity(string friendlyName, string typeIdGuid, string description)
        {
            Identity = new PSPluginDescriptor
                           {
                               Name = friendlyName,
                               TypeId = new Guid(typeIdGuid),
                               Description = description
                           };
        }

        public void SetCriticalFailure(Guid id)
        {
            CriticalFailure = true;
            CriticalFailureDetails = new PSCriticalFailureDetails
                                         {
                                             Id = id
                                         };
        }
    }
}
